/*************************************************************************
*																		 *
*				code by Miles Chen (v-mich) 2007.10.8					 *
*																		 *
*																		 *
*************************************************************************/

#pragma once

#include "commdef-int.h"

namespace System{namespace Diagnostics{namespace Process{

#include <psapi.h>
#pragma comment (lib,"psapi.lib")

	BOOL Start(LPCWSTR lpFile, LPWSTR lpCommandLine, LPPROCESS_INFORMATION ppi)
	{
		STARTUPINFO si;

		ZeroMemory(&si, sizeof(si));
		ZeroMemory(ppi, sizeof(PPROCESS_INFORMATION));
		si.cb = sizeof(si);

		return CreateProcessW(
			lpFile,
			lpCommandLine,
			NULL,
			NULL,
			TRUE,
			CREATE_DEFAULT_ERROR_MODE,
			NULL,
			NULL,
			&si,
			ppi);
	}

	BOOL Start(LPCWSTR lpFile, LPPROCESS_INFORMATION ppi)
	{
		return Start(lpFile, NULL, ppi);
	}

	BOOL Kill(HANDLE hProcess)
	{
		return TerminateProcess(hProcess, -1);
	}

	BOOL GetProcessBaseName(HANDLE hProcess, LPWSTR lpszBaseName, int nSize)
	{
		return GetModuleBaseNameW(hProcess, NULL, lpszBaseName, nSize);
	}

	BOOL GetProcessBaseName(DWORD dwProcID, LPWSTR lpszBaseName, int nSize)
	{
		BOOL result = TRUE;
		HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwProcID);
		if(!hProcess)
		{
			result = FALSE;
			goto exit;
		}

		if(!GetProcessBaseName(hProcess, lpszBaseName, nSize))
		{
			result = FALSE;
			goto exit;
		}

exit:
		if(hProcess)
			CloseHandle(hProcess);
		return result;
	}

}}}